<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { deleteArticleApi, getArticleListApi } from '@/api'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
import { ElMessage } from 'element-plus'

// 假数据
const articleList = ref([])
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 总数据条数
const total = ref(0)
// load
const loading = ref(false)

// 页面条数改变
const handleSizeChange = async (size) => {
  console.log(size)
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
// 当前页改变
const handleCurrentChange = (page) => {
  console.log(page)
  params.value.pagenum = page
  getArticleList()
}

const getArticleList = async () => {
  loading.value = true
  const { data } = await getArticleListApi(params.value)
  articleList.value = data.data
  total.value = data.total
  loading.value = false
}
getArticleList()
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const onDelete = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await deleteArticleApi(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}

// 抽屉
const articleEditRef = ref()
const postArticle = () => {
  articleEditRef.value.open({})
}
const onEdit = (row) => {
  articleEditRef.value.open(row)
}
const changeSuccess = (type) => {
  if (type === 'add') {
    params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="postArticle">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类">
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select
          v-model="params.state"
          placeholder="请选择"
          clearable
          style="width: 220px"
        >
          <el-option label="已发布" value="已发布" />
          <el-option label="草稿" value="草稿" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题" width="220">
        <template #default="{ row }">
          <el-link :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" width="180" />
      <el-table-column label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            size="small"
            @click="onEdit(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            size="small"
            @click="onDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[1, 2, 3, 5]"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉 -->
    <article-edit ref="articleEditRef" @success="changeSuccess"></article-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
